home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club (Business) 1997 July / Software of the Month Club - Business (Volume 239) (July 1997).iso / pc / code / shared.dxr / 04481_Game Save-Preferences Code.ls < prev    next >
Encoding:
Text File  |  1996-02-21  |  14.7 KB  |  455 lines

  1. global gNextEnvLetter, gNextDIRFile, gMIDIPlayTRUE, gInitalized, noteObjs, windowsMIDIXObj, xNAVXOBJ, xCURXOBJ, gCPU, gfileSep, gRootPath, gHDpath, gXobjs, gXOBJPath, gCDpath, gCDName, gdirtyGame, prefsPath, startPath, miscXObjectPath, utilXObjectPath, miscXobj, saveDataObj, myUtil, savedFilesPath, XObjectPath, extension, fileioXObjectPath, IOerrors, gCodePath, void
  2.  
  3. on massagegStatesForSave_Dan dynObjList
  4.   global gStates
  5.   if voidp(dynObjList) then
  6.     set dynObjList to getaProp(gStates, #Dynamic)
  7.   end if
  8.   set objtempList to [:]
  9.   set AlltempList to [:]
  10.   repeat with ObjRef in dynObjList
  11.     set obj to getaProp(gOBJECTS, ObjRef)
  12.     set DynamicList to getaProp(obj, #Dynamic)
  13.     set objtempList to [:]
  14.     if voidp(DynamicList) then
  15.       put "SaveError DynamicList VOID" && "#" & ObjRef
  16.       next repeat
  17.     end if
  18.     if count(DynamicList) = 0 then
  19.       put "SaveError No Values in DynamicList" && "#" & ObjRef
  20.       next repeat
  21.     end if
  22.     repeat with aProp in DynamicList
  23.       set aValue to getaProp(obj, aProp)
  24.       if voidp(aValue) then
  25.         set aValue to #voidp
  26.         put "SaveError Void in" && "#" & ObjRef && "property" && aProp
  27.       end if
  28.       setaProp(objtempList, aProp, aValue)
  29.     end repeat
  30.     setaProp(AlltempList, ObjRef, objtempList)
  31.   end repeat
  32.   setaProp(gStates, #dynamicData, AlltempList)
  33.   return AlltempList
  34. end
  35.  
  36. on massagegStatesOnOpen_Dan
  37.   global void, gStates
  38.   set dynamicDataList to getaProp(gStates, #dynamicData)
  39.   set C to count(dynamicDataList)
  40.   repeat with RefAT = 1 to C
  41.     set ObjRef to getPropAt(dynamicDataList, RefAT)
  42.     set ValueSet to getAt(dynamicDataList, RefAT)
  43.     set obj to getaProp(gOBJECTS, ObjRef)
  44.     repeat with PropAT = 1 to count(ValueSet)
  45.       set aProp to getPropAt(ValueSet, PropAT)
  46.       set aValue to getAt(ValueSet, PropAT)
  47.       if aValue = #voidp then
  48.         deleteProp(obj, aProp)
  49.         next repeat
  50.       end if
  51.       setaProp(obj, aProp, aValue)
  52.     end repeat
  53.   end repeat
  54. end
  55.  
  56. on SetSaveFilename_DAN
  57.   set saveFileName to line 1 of field getaProp(getaProp(getaProp(gOBJECTS, #SaveNameText), #cast), #text)
  58.   if length(saveFileName) < 1 then
  59.     set saveFileName to "CHAOS"
  60.   end if
  61.   if gCPU = #mpc then
  62.     set DOSname to makeDOSfilename(saveFileName)
  63.     set saveFileName to char 1 to 8 of DOSname & "." & saveDataType()
  64.   end if
  65.   return saveFileName
  66. end
  67.  
  68. on saveGameData_DAN
  69.   global void, gStates, gSoundChannels, gsoundLevel, gCDNotes, gNotesToPlay, gNotes, gMidiManager, gMidiStartTime, gMidiInstruments
  70.   setaProp(gStates, #curKey, getaProp(gCurKeyList, #RefName))
  71.   set saveFileName to SetSaveFilename_DAN()
  72.   setaProp(gStates, #gCurrentGameName, saveFileName)
  73.   set savedGameGlobals to [#gStates: gStates, #gSoundChannels: gSoundChannels, #gsoundLevel: gsoundLevel, #gCDNotes: gCDNotes, #gNotesToPlay: gNotesToPlay, #gNotes: gNotes, #gMidiManager: gMidiManager, #gMidiStartTime: gMidiStartTime, #gMidiInstruments: gMidiInstruments]
  74.   set OutputData to dumpGlobals_Dan(savedGameGlobals)
  75.   set fileData to ChaosVersionString()
  76.   put string(OutputData) into line 2 of fileData
  77.   set promptForName to 0
  78.   saveData_DAN(OutputData, saveFileName, fileData, promptForName)
  79.   if getOne(gScreenState, #SaveDialogBox) > 0 then
  80.     CloseSaveUNVDialog()
  81.   end if
  82. end
  83.  
  84. on dumpGlobals_Dan globalList
  85.   global void, gStates
  86.   set DyanmicData to massagegStatesForSave_Dan(getaProp(gStates, #Dynamic))
  87.   set C to count(globalList)
  88.   repeat with refnum = 1 to C
  89.     if voidp(getAt(globalList, refnum)) then
  90.       setAt(globalList, refnum, #voidp)
  91.     end if
  92.   end repeat
  93.   set returnlist to value(string(globalList))
  94.   setaProp(returnlist, #dynamicData, DyanmicData)
  95.   return returnlist
  96. end
  97.  
  98. on saveData_DAN OutputData, saveFileName, fileData, promptForName
  99.   set MockMes to "Chaos:Code:Debug:mockmsg3"
  100.   if the visible of window MockMes = 1 then
  101.     tell window MockMes
  102.       SaveGameCheckWindow(OutputData)
  103.     end tell
  104.   else
  105.     put "Current Save File will be"
  106.     repeat with refnum = 1 to count(OutputData)
  107.       put getPropAt(OutputData, refnum) && getAt(OutputData, refnum)
  108.     end repeat
  109.   end if
  110. end
  111.  
  112. on SaveGameData
  113.   global gStates
  114.   set gdirtyGame to 0
  115.   setaProp(gStates, #curKey, getaProp(gCurKeyList, #RefName))
  116.   set saveFileName to line 1 of field getaProp(getaProp(getaProp(gOBJECTS, #SaveNameText), #cast), #text)
  117.   if length(saveFileName) < 1 then
  118.     set saveFileName to "CHAOS"
  119.   end if
  120.   if gCPU = #mpc then
  121.     set DOSname to makeDOSfilename(saveFileName)
  122.     set saveFileName to char 1 to 8 of DOSname & "." & saveDataType()
  123.   end if
  124.   setaProp(gStates, #gCurrentGameName, saveFileName)
  125.   set savedGameGlobals to [#gStates: "a dynamic list of the current state of the game.", #gSoundChannels: "a dynamic list of the current state of sound.", #gsoundLevel: "sound level.", #gCDNotes: "a hit record from the radiostation.", #gNotesToPlay: "the radiostation notes before a CD is cut.", #gNotes: "the radiostation notes before a CD is cut.", #gMidiManager: "something", #gMidiStartTime: "something", #gMidiInstruments: "something"]
  126.   set savedGameData to dumpGlobals(savedGameGlobals)
  127.   set fileData to ChaosVersionString()
  128.   put string(savedGameData) into line 2 of fileData
  129.   set promptForName to 0
  130.   if saveData(saveFileName, fileData, promptForName) then
  131.     if getOne(gScreenState, #SaveDialogBox) > 0 then
  132.       CloseSaveUNVDialog()
  133.     end if
  134.     if getaProp(getaProp(gOBJECTS, #QuitYetDialog), #gQuitAfter) = 1 then
  135.       halt()
  136.     end if
  137.   else
  138.     displayChaosError(#warning, "There was an error writing to the file. Please save your game under another name and quit")
  139.   end if
  140. end
  141.  
  142. on loadGameData
  143.   global gStates
  144.   set saveFileName to line 1 of field getaProp(getaProp(getaProp(gOBJECTS, #SaveNameText), #cast), #text)
  145.   set promptForName to 0
  146.   set savedGameData to loadData(saveFileName, promptForName)
  147.   if (savedGameData <> EMPTY) and not voidp(savedGameData) then
  148.     if line 1 of savedGameData = ChaosVersionString() then
  149.       delete line 1 of savedGameData
  150.       restoreGlobals(savedGameData)
  151.       massagegStatesOnOpen()
  152.       return 1
  153.     else
  154.       displayChaosError(#severe, "This file does not contain saved Chaos game data, or is old or damaged.")
  155.       return 0
  156.     end if
  157.   else
  158.     put "Couldn't get any data from the file. Something's screwed up."
  159.     return 0
  160.   end if
  161.   put "Something's screwed up."
  162. end
  163.  
  164. on ChaosVersionString
  165.   set fileData to "Gray Matter Studios, version 11. kam stewart, system architect"
  166.   return fileData
  167. end
  168.  
  169. on ChaosPrefVerification
  170.   set Data to "program preferences"
  171.   return Data
  172. end
  173.  
  174. on SetPrefFilename
  175.   set prefFileName to "CHAOS" & "." & prefDataType()
  176.   return prefFileName
  177. end
  178.  
  179. on savePrefData
  180.   global myMidiDevice, gLimitPreLoad
  181.   set saveFileName to SetPrefFilename()
  182.   if voidp(myMidiDevice) then
  183.     set myMidiDevice to voidDatasymbol()
  184.   end if
  185.   if voidp(gLimitPreLoad) then
  186.     set gLimitPreLoad to voidDatasymbol()
  187.   end if
  188.   set ChaosGlobal to [#myMidiDevice: myMidiDevice, #gLimitPreLoad: gLimitPreLoad]
  189.   set prefData to ChaosPrefVerification()
  190.   put string(ChaosGlobal) into line 2 of prefData
  191.   set promptForName to 0
  192.   return saveData(saveFileName, prefData, promptForName)
  193. end
  194.  
  195. on loadPrefData
  196.   global myMidiDevice, gLimitPreLoad
  197.   set saveFileName to SetPrefFilename()
  198.   set promptForName to 0
  199.   set prefData to loadData(saveFileName, promptForName)
  200.   if (prefData <> EMPTY) and not voidp(prefData) then
  201.     if line 1 of prefData = ChaosPrefVerification() then
  202.       delete line 1 of prefData
  203.       restoreGlobals(prefData)
  204.       return 1
  205.     else
  206.       displayChaosError(#severe, "The prefs file is damaged. You must restart and re-configure your MIDI settings.")
  207.       return 0
  208.     end if
  209.   else
  210.     set myMidiDevice to 1
  211.     set gLimitPreLoad to 0
  212.     return 0
  213.   end if
  214. end
  215.  
  216. on restoreLastGame
  217.   global gStates
  218.   if ilk(aPropertyListVariable, #linearList) then
  219.     set aPropertyListVariable to [:]
  220.   end if
  221. end
  222.  
  223. on dumpGlobals globalList
  224.   massagegStatesForSave()
  225.   set listResult to [:]
  226.   set howManyGlobals to count(globalList)
  227.   repeat with globalNumber = 1 to howManyGlobals
  228.     set globalName to getPropAt(globalList, globalNumber)
  229.     set globalData to value(string(globalName))
  230.     if voidp(globalData) then
  231.       set globalData to voidDatasymbol()
  232.     end if
  233.     addProp(listResult, globalName, globalData)
  234.   end repeat
  235.   return listResult
  236. end
  237.  
  238. on restoreGlobals listText
  239.   global gStates, gSoundChannels, gsoundLevel, gCDNotes, gNotesToPlay, gNotes, gMidiManager, gMidiStartTime, gMidiInstruments, myMidiDevice, gLimitPreLoad
  240.   set globalList to value(listText)
  241.   if voidp(globalList) then
  242.     alert("Data was bad!! Probably a '<void>' somewhere. please quit.")
  243.     exit
  244.   end if
  245.   set howManyGlobals to count(globalList)
  246.   repeat with globalNumber = 1 to howManyGlobals
  247.     set globalName to getPropAt(globalList, globalNumber)
  248.     set globalData to getaProp(globalList, globalName)
  249.     if ilk(globalData, #list) then
  250.       set dataType to #list
  251.     else
  252.       set dataType to ilk(globalData)
  253.     end if
  254.     if dataType = #integer then
  255.       set cmdString to "set" && globalName && "=" && string(getaProp(globalList, globalName))
  256.     else
  257.       if dataType = #float then
  258.         set cmdString to "set" && globalName && "=" && string(getaProp(globalList, globalName))
  259.       else
  260.         if dataType = #symbol then
  261.           set cmdString to "set" && globalName && "= value(" & "#" & getaProp(globalList, globalName) & ")"
  262.         else
  263.           if dataType = #string then
  264.             set cmdString to "set" && globalName && "=" && QUOTE & getaProp(globalList, globalName) & QUOTE
  265.           else
  266.             if dataType = #list then
  267.               set cmdString to "set" && globalName && "=" && string(getaProp(globalList, globalName))
  268.             end if
  269.           end if
  270.         end if
  271.       end if
  272.     end if
  273.     do(cmdString)
  274.   end repeat
  275. end
  276.  
  277. on startChaos
  278.   global gStates
  279.   if voidp(savedFilesPath) then
  280.     InitSystemVars()
  281.   end if
  282.   if savedGamesExist() then
  283.     OpenNewGameDialog()
  284.   else
  285.     go("Intro QT")
  286.   end if
  287. end
  288.  
  289. on loadData fileName, promptForName
  290.   InitSystemVars()
  291.   if objectp(myFileIO) then
  292.     myFileIO(mdispose)
  293.   end if
  294.   if promptForName then
  295.     set myFileIO to FileIO(mnew, "?read", saveDataType())
  296.   else
  297.     set myFileIO to FileIO(mnew, "read", savedFilesPath & gfileSep & fileName)
  298.   end if
  299.   if objectp(myFileIO) then
  300.     set dataStream to myFileIO(mReadFile)
  301.     if objectp(myFileIO) then
  302.       myFileIO(mdispose)
  303.     end if
  304.     return dataStream
  305.   else
  306.     if not voidp(myFileIO) and (myFileIO <> -43) then
  307.       set errMsg to getaProp(IOerrors, abs(myFileIO))
  308.       if voidp(errMsg) then
  309.         displayChaosError(#warning, "Error occurred: Code" && string(myFileIO))
  310.       else
  311.         displayChaosError(#warning, "Error occurred:" && errMsg)
  312.       end if
  313.     end if
  314.     if objectp(myFileIO) then
  315.       myFileIO(mdispose)
  316.     end if
  317.     return EMPTY
  318.   end if
  319. end
  320.  
  321. on saveData saveFileName, fileData, promptForName
  322.   set volumeName to miscXobj(mBootName)
  323.   set diskSpace to miscXobj(mSpaceOnVol, volumeName)
  324.   if diskSpace < 80000 then
  325.     displayChaosError(#warning, "Your disk is too full to save your Chaos game data.  There are only" && string(diskSpace) && "bytes remaining on drive" && volumeName & ".")
  326.     exit
  327.   end if
  328.   if miscXobj(mFileExists, savedFilesPath & gfileSep & saveFileName) then
  329.     if objectp(myFileIO) then
  330.       myFileIO(mdispose)
  331.     end if
  332.     set myFileIO to FileIO(mnew, "read", savedFilesPath & gfileSep & saveFileName)
  333.     myFileIO(mDelete)
  334.   end if
  335.   if objectp(myFileIO) then
  336.     myFileIO(mdispose)
  337.   end if
  338.   if promptForName then
  339.     if objectp(saveDataObj) then
  340.       saveDataObj(mdispose)
  341.     end if
  342.     if the machineType = 256 then
  343.       set saveDataObj to movUtils(mnew)
  344.     else
  345.       set saveDataObj to MovieUtilities(mnew)
  346.     end if
  347.     saveDataObj(mSetDefaultPath, savedFilesPath)
  348.     set myFileIO to FileIO(mnew, "?write", saveFileName)
  349.     saveDataObj(mdispose)
  350.   else
  351.     put savedFilesPath & gfileSep & saveFileName
  352.     set myFileIO to FileIO(mnew, "write", savedFilesPath & gfileSep & saveFileName)
  353.   end if
  354.   if objectp(myFileIO) then
  355.     if gCPU = #MAC then
  356.       myFileIO(mSetFinderInfo, saveDataType(), "ttxt")
  357.     end if
  358.     myFileIO(mWriteString, fileData)
  359.     if objectp(myFileIO) then
  360.       myFileIO(mdispose)
  361.     end if
  362.     return 1
  363.   else
  364.     if not voidp(myFileIO) then
  365.       if myFileIO <> -43 then
  366.         set errMsg to getaProp(IOerrors, abs(myFileIO))
  367.         if voidp(errMsg) then
  368.           displayChaosError(#warning, "An error occurred while saving your Chaos game data:" & RETURN & RETURN & "Code" && string(myFileIO))
  369.         else
  370.           displayChaosError(#warning, "An error occurred while saving your Chaos game data:" & RETURN & RETURN & errMsg)
  371.         end if
  372.       end if
  373.     else
  374.       displayChaosError(#warning, "void instance of fileIO.")
  375.     end if
  376.     if objectp(myFileIO) then
  377.       myFileIO(mdispose)
  378.     end if
  379.     return 0
  380.   end if
  381. end
  382.  
  383. on makeDOSfilename str
  384.   set theCount to length(str)
  385.   set theResult to EMPTY
  386.   repeat with X = 1 to theCount
  387.     set theChar to char X of str
  388.     if "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_" contains theChar then
  389.       put theChar after theResult
  390.     end if
  391.   end repeat
  392.   return theResult
  393. end
  394.  
  395. on saveDataType
  396.   if gCPU = #MAC then
  397.     set signature to "KAOS"
  398.   else
  399.     set signature to "SVG"
  400.   end if
  401.   return signature
  402. end
  403.  
  404. on prefDataType
  405.   set signature to "PRF"
  406.   return signature
  407. end
  408.  
  409. on voidDatasymbol
  410.   set voidsymbol to [#void]
  411.   return voidsymbol
  412. end
  413.  
  414. on LoadXObjects
  415.   set IOerrors to [33: "File directory full", 34: "Volume full", 35: "Volume not found", 36: "I/O Error", 37: "Bad file name", 38: "File not open", 42: "Too many files open", 43: "File not found", 49: "File open with write permission", 56: "No such drive", 65: "No disk in drive", 120: "Directory not found"]
  416.   if the machineType = 256 then
  417.     set cpu to #mpc
  418.     openXLib(gCodePath & "FILEIO.DLL")
  419.     openXLib(gCodePath & "MovUtils.DLL")
  420.     openXLib(gCodePath & "MISC_X.DLL")
  421.   else
  422.     set cpu to #MAC
  423.     if not objectp(factory("MovieUtilities XObj")) then
  424.       openXLib(gCodePath & "MovieUtilities XObj")
  425.     end if
  426.     if not objectp(factory("Misc_X XObj")) then
  427.       openXLib(gCodePath & "Misc_X XObj")
  428.     end if
  429.     if not objectp(factory("PlayNote.Xobj")) then
  430.       openXLib(gCodePath & "PlayNote.Xobj")
  431.     end if
  432.   end if
  433. end
  434.  
  435. on savePreferences
  436.   global myMidiDevice, gLimitPreLoad
  437.   set prefsGlobals to [#myMidiDevice: "the midi driver the user chooses", #gLimitPreLoad: "Turbo-mode, if off skips preLoad on large frames"]
  438.   set prefsData to dumpGlobals(prefsGlobals)
  439.   saveData("CHAOS.PRF", string(prefsData))
  440. end
  441.  
  442. on deleteGame whichGame
  443.   global savedFilesPath, gfileSep
  444.   if miscXobj(mFileExists, savedFilesPath & gfileSep & whichGame) then
  445.     if objectp(myFileIO) then
  446.       myFileIO(mdispose)
  447.     end if
  448.     set myFileIO to FileIO(mnew, "read", savedFilesPath & gfileSep & whichGame)
  449.     myFileIO(mDelete)
  450.     if objectp(myFileIO) then
  451.       myFileIO(mdispose)
  452.     end if
  453.   end if
  454. end
  455.